package model.bo;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import model.dao.ChangeAvatarDAO;
import org.apache.commons.fileupload.FileItem;


public class ChangeAvatarBO {

	public String changeAvatar(List formItems, Iterator iter, String uploadPath,String path, int userID) {
		ChangeAvatarDAO changeAvatarDAO = new ChangeAvatarDAO();
		String fileName = "";
		File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
		try {
		while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            // processes only fields that are not form fields
            if (!item.isFormField()) {
                fileName = new File(item.getName()).getName();
                String filePath = uploadPath + File.separator + fileName;
                File storeFile = new File(filePath);
                item.write(storeFile);
            }
        }
		} catch (Exception e) {
			// do not something
		} finally{
			// do not something
		}
		changeAvatarDAO.changeAvatar(fileName,userID);
		return changeAvatarDAO.changeAvatar(fileName,userID);
	}


}
